import * as types from '../mutation-types'
import router from '../../router/index'
import { baseApi } from '@/api'

import utils from '@/utils/common'

export default {
    state: {
        loading: false,
        userInfo: null,
        friendList: null,
        groupList: null,
        currentUser: null,
        currentGroup: null,
        online: true,
        reConnect: false
    },
    mutations: {
        [types.SHOW_LOADING](state) {
            state.loading = true
        },
        [types.HIDE_LOADING](state) {
            state.loading = false
        },
        [types.SET_USER_INFO](state, res) {
            state.userInfo = res
        },
        [types.SET_FRIEND_LIST](state, res) {
            state.friendList = res
        },
        [types.SET_GROUP_LIST](state, res) {
            state.groupList = res
        },
        [types.UPDATE_GROUP_STATUS](state, res) {
            let gropArr = state.groupList.filter(el => { return el.id == res.groupid })
            if (gropArr.length > 0) {
                let group = gropArr[0]
                group.isRemove = res.isRemove
                let newObj = Object.assign({}, group)
                var findIndex = state.groupList.findIndex(el => { return el.id == res.groupid })
                state.groupList.splice(findIndex, 1, newObj)
            }
        },
        [types.INSERT_GROUP_LIST](state, res) {
            // console.log('插入组群')
            state.groupList.push(res)
        },
        [types.INSERT_FRIEND_LIST](state, res) {
            // console.log('插入好友')
            for (let key in res) {
                let value = res[key]
                if (state.friendList[key]) {
                    state.friendList[key].push(value[0])
                } else {
                    state.friendList[key] = value
                }
            }
            let newObj = Object.assign({}, state.friendList)
            state.friendList = utils.dataLetterSort(newObj)
        },
        [types.SET_CURRENT_USER](state, res) {
            state.currentUser = res
        },
        [types.SET_CURRENT_GROUP](state, res) {
            state.currentGroup = res
        },
        [types.RESET_BASE](state) {
            state.userInfo = null
            state.friendList = null
            state.groupList = null
            state.currentUser = null
            state.currentGroup = null
        },
        [types.SET_GROUP_STATUS](state, res) {
            for (let i = 0; i < state.groupList.length; i++) {
                if (state.groupList[i].id == parseInt(res.id)) {
                    state.groupList[i].status = res.status
                }
            }
        },
        [types.CHANGE_ONLINE_STATUS](state, res) {
            state.online = res
        },
        [types.CHANGE_RECONNECT_STATUS](state, res) {
            state.reConnect = res
        }
    },
    actions: {
        showLoading({ commit }) {
            commit('SHOW_LOADING')
        },
        hideLoading({ commit }) {
            commit('HIDE_LOADING')
        },
        initApp({ commit }) {
            commit('SHOW_LOADING')
            baseApi.getFriendList().then(res => {
                if (res.code == 200) {
                    if (res.data) {
                        for (let key in res.data) {
                            let list = res.data[key]
                            if (list && list.length > 0) {
                                for (let i = 0; i < list.length; i++) {
                                    if (list[i].usericon) {
                                        list[i].bigusericon = list[i].usericon + '?x-oss-process=image/resize,h_150'
                                        list[i].usericon = list[i].usericon + '?x-oss-process=image/resize,h_40'
                                    }
                                }
                            }
                        }
                    }
                    commit('SET_FRIEND_LIST', utils.dataLetterSort(res.data))
                    return baseApi.getGroupList()
                }
            }).then(res => {
                if (res.code == 200) {
                    commit('SET_GROUP_LIST', res.data)
                }
                commit('HIDE_LOADING')
            })
        },
        getUserInfo({ dispatch, commit }, res) {
            if (res) {
                commit('SET_USER_INFO', res)
            } else {
                baseApi.getUserInfo().then(res => {
                    commit('SET_USER_INFO', res.data)
                    if (res.code == 200) {
                        dispatch('initApp')
                        router.push({ path: '/message' })
                    } else {
                        router.push({ path: '/login' })
                    }
                })
            }
        },
        insertGroup({ commit }, res) {
            commit('INSERT_GROUP_LIST', res)
        },
        updateGroupStatus({ commit }, res) {
            commit('UPDATE_GROUP_STATUS', res)
        },
        insertFriend({ commit }, res) {
            commit('INSERT_FRIEND_LIST', res)
        },
        setCurrentUser({ commit }, res) {
            commit('SET_CURRENT_USER', res)
        },
        setCurrentGroup({ commit }, res) {
            commit('SET_CURRENT_GROUP', res)
        },
        resetBase({ commit }) {
            commit('RESET_BASE')
        },
        setGroupStatus({ commit }, res) {
            commit('SET_GROUP_STATUS', res)
        },
        changeOnlineStatus({ commit }, res) {
            commit('CHANGE_ONLINE_STATUS', res)
        },
        changeReconnectStatus({ commit }, res) {
            commit('CHANGE_RECONNECT_STATUS', res)
        }
    }
}
